package com.zm.spring.dependency.injection.construct.demo;

import com.zm.demo.spring.ioc.po.UserPO;
import com.zm.spring.dependency.injection.entity.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

public class AnnotationDependencySetterInjectionDemo {

    @Autowired
    private UserHolder userHolder;

    public static void main(String[] args) {

        AnnotationConfigApplicationContext annotationConfigApplicationContext
                = new AnnotationConfigApplicationContext();
        annotationConfigApplicationContext.register(AnnotationDependencySetterInjectionDemo.class);
        BeanDefinitionReader reader = new XmlBeanDefinitionReader(annotationConfigApplicationContext);
        reader.loadBeanDefinitions("classpath:/META-INF/spring-ioc.xml");
        annotationConfigApplicationContext.refresh();
        AnnotationDependencySetterInjectionDemo demo = annotationConfigApplicationContext.getBean(AnnotationDependencySetterInjectionDemo.class);
        System.out.println(demo.userHolder);

        UserHolder userHolder1 = annotationConfigApplicationContext.getBean(UserHolder.class);
        System.out.println(userHolder1);

        System.out.println(demo.userHolder == userHolder1);


        annotationConfigApplicationContext.close();

    }

    @Bean
    public UserHolder createUserHolder(UserPO userPO) {
        return new UserHolder(userPO);
    }

}
